package com.aaa.custom.service.orderService;


import com.aaa.custom.dao.orderDao.OrderDao;
import com.aaa.custom.service.productsService.ProductsService;
import com.aaa.entity.cart_order.kwc.CartList;
import com.aaa.entity.cart_order.zxh.Order;
import com.aaa.entity.cart_order.zxh.Order_Item;
import com.aaa.util.RedissLockUtil;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.codingapi.txlcn.tc.support.DTXUserControls;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * fileName:OrderServiceImpl
 * description:
 * author:zxh
 * createTime:2020/1/13 8:56
 * version:1.0.0
 */
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderDao orderDao;
    @Autowired
    private ProductsService productsService;

    private final String _lock="_lock";
    //@LcnTransaction//分布式事务
    //@Transactional//本地事务
    @Override
    public List<Order> byMember_idAndShop_idGetShop(Long id,Long member_id, Long shop_id,String order_sn) {
        List<Order> orderList=orderDao.byMember_idAndShop_idGetShop(id,member_id,shop_id,order_sn);
        for (int i = 0; i <orderList.size() ; i++) {
            orderList.get(i).setProductLength(orderList.get(i).getOrder_items().size());
        }
        return orderList;
    }

    @Override
    public List<Order_Item> byOrder_snGetOrder(String order_sn) {
        return orderDao.byOrder_snGetOrder(order_sn);
    }
    @LcnTransaction
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delectOrderByID(Long id, Integer delete_status, Integer confirm_status, Integer status) {
        RedissLockUtil.lock(_lock,TimeUnit.MINUTES,3);
        Order order=orderDao.byMember_idAndShop_idGetShop(id,null,null,null).get(0);
        if ((order.getStatus()==0&&status==4)||(status==5&&order.getStatus()==0)) {
            for (Order_Item order_Item : order.getOrder_items()
            ) {
                productsService.ProductStockAddbyId(order_Item.getProduct_id(),order_Item.getProduct_quantity());
            }
        }
        RedissLockUtil.unlock(_lock);
        return orderDao.delectOrderByID(id,delete_status,confirm_status,status);
    }
    @LcnTransaction
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int add_oms_order_item(Map orderProducts) {

        RedissLockUtil.lock(_lock, TimeUnit.MINUTES,3);
        List<CartList> cartLists= (List<CartList>) orderProducts.get("cartLists");
        for (CartList cart:cartLists
             ) {
            try {
                productsService.ProductStockSubtractbyId(cart.getProduct().getProduct_id(),cart.getQuantity());
            } catch (Exception e) {
                //库存不足手动回滚
                DTXUserControls.rollbackCurrentGroup();
            }
        }
        RedissLockUtil.unlock(_lock);
        //orderDao.add_oms_order();
        //orderDao.add_oms_order();
        return 1;
    }


}
